<template>
	<view class="wrapper">
		<view class="price">￥{{msgData.totalAmount || 0}}</view>
		<view class="title">支付方式</view>
		<view class="pay">
			<view class="pay-tips">
				<!-- <u-icon name="weixin-fill" size="28" color="green" ></u-icon> -->
				<image src="/static/wx-icon.png" mode="aspectFit" class="pay-image"></image>
				<text class="pay-tips-text">微信支付</text>
			</view>
			<view class="icon-round"></view>
		</view>

		<view class="submit" @click="handleSubmit">确认付款</view>
		<!-- <button @click="handleReset">重新连接</button> -->
	</view>
</template>
<script>
	import {
		payCallback,
		setTicketPaySuccess
	} from "@/http";
	import {
		payWcOrder
	} from "@/culturalCreativity/http/common.js";
	export default {
		data() {
			return {
				msgData: {}
			}
		},
		onLoad() {
			this.msgData = uni.getStorageSync('msg_data');
		},
		onUnload() { //修改默认返回
			let msgData = uni.getStorageSync("msg_data");
			if (msgData.fileName == 'culturalCreativity') return;
			uni.setStorageSync("webview_url", msgData.url);
			uni.reLaunch({
				url: "/webview/index"
			});
		},
		methods: {
			async handleSubmit() {
				if (this.msgData.fileName == 'culturalCreativity') {
					let res = await payWcOrder({
						totalPrice: this.msgData.totalAmount,
						orderId: this.msgData.orderId
					}) 
					console.log('支付结果:::',res);
					uni.redirectTo({
						url:"/culturalCreativity/my/order?current=2"
					});
					return;
				}
				uni.getProvider({
					service: "payment",
					success: res => {
						let payData = {
							timeStamp: this.msgData.timeStamp,
							package: this.msgData.package,
							paySign: this.msgData.paySign,
							openId: this.msgData.openId,
							outTradeNo: this.msgData.outTradeNo,
							nonceStr: this.msgData.nonceStr,
							jdOrderId: this.msgData.jdOrderId,
							signType: "RSA"
						}
						uni.requestPayment({
							...payData,
							success: sres => {
								console.log('支付成功', sres);
								payCallback({
									thirdOrderId: this.msgData.thirdOrderId,
									thirdType: String(this.msgData.thirdType),
									wxPayType: String(2)
								}).then(res => {
									if (this.msgData.thirdType == 2) {
										uni.setStorageSync("webview_url", res.data.data)
										uni.reLaunch({
											url: "/webview/index"
										});
										return;
									};
									if (this.msgData.thirdType != 1) {
										return uni.reLaunch({
											url: "/pages/home/index"
										});
									};

									if (this.msgData.thirdType == 1) {
										if (res.data.data.indexOf("错误") != -1) {
											uni.showToast({
												title: res.data.data,
												icon: "error"
											});
											setTimeout(() => {
												uni.reLaunch({
													url: "/pages/home/index"
												});
											}, 2000);
											return;
										}
									}

									uni.setStorageSync("webview_url", res.data.data)
									uni.reLaunch({
										url: "/webview/index"
									});
								})
							},
							fail: err => {

								if (err.errMsg.indexOf("cancel") != -1) {
									payCallback({
										thirdOrderId: this.msgData.thirdOrderId,
										thirdType: String(this.msgData.thirdType),
										wxPayType: String(3)
									}).then(res => {

										if (this.msgData.thirdType == 2) {
											uni.setStorageSync("webview_url", res.data
												.data)
											uni.reLaunch({
												url: "/webview/index"
											});
											return;
										};


										if (this.msgData.thirdType != 1) {
											return uni.reLaunch({
												url: "/pages/home/index"
											});
										};

										if (this.msgData.thirdType == 1) {
											if (res.data.data.indexOf("错误") != -1) {
												uni.showToast({
													title: res.data.data,
													icon: "error"
												});
												setTimeout(() => {
													uni.reLaunch({
														url: "/pages/home/index"
													});
												}, 2000);
												return;
											}
										}

										uni.setStorageSync("webview_url", res.data.data)
										uni.reLaunch({
											url: "/webview/index"
										});
									})
								} else {
									payCallback({
										thirdOrderId: this.msgData.thirdOrderId,
										thirdType: String(this.msgData.thirdType),
										wxPayType: String(4)
									}).then(res => {
										if (this.msgData.thirdType == 2) {
											uni.setStorageSync("webview_url", res.data
												.data)
											uni.reLaunch({
												url: "/webview/index"
											});
											return;
										};

										if (this.msgData.thirdType != 1) {
											return uni.reLaunch({
												url: "/pages/home/index"
											});
										};

										if (this.msgData.thirdType == 1) {
											if (res.data.data.indexOf("错误") != -1) {
												uni.showToast({
													title: res.data.data,
													icon: "error"
												});
												setTimeout(() => {
													uni.reLaunch({
														url: "/pages/home/index"
													});
												}, 2000);
												return;
											}
										}


										uni.setStorageSync("webview_url", res.data.data)
										uni.reLaunch({
											url: "/webview/index"
										});
									})
								}
							}
						});
					}
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.wrapper {
		padding: 0 20rpx;

		.pay {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 18rpx;

			.pay-tips {
				display: flex;
				align-items: center;

				.pay-image {
					width: 45rpx;
					height: 45rpx;
				}

				.pay-tips-text {
					margin-left: 10rpx;
					color: #333333;
					font-size: 24rpx;
				}
			}

			.icon-round {
				width: 14rpx;
				height: 14rpx;
				border-radius: 50%;
				background-color: green;
				position: relative;

				&::after {
					content: "";
					position: absolute;
					left: -6rpx;
					top: -6rpx;
					right: -6rpx;
					bottom: -6rpx;
					border-radius: 50%;
					border: 1px solid green;
				}
			}
		}

		.price {
			font-size: 50rpx;
			font-weight: bold;
			color: red;
			text-align: center;
			margin-top: 30rpx;
			margin-bottom: 40rpx;
		}

		.title {
			font-size: 36rpx;
			font-weight: bold;
			padding-bottom: 12rpx;
			border-bottom: 1rpx solid #f2f2f2;
		}

		.submit {
			background-color: #ff9933;
			width: 580rpx;
			height: 80rpx;
			border-radius: 8rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 26rpx;
			font-weight: bold;
			color: #FFFFFF;
			margin: 0 auto;
			margin-top: 600rpx;
		}
	}
</style>
